package com.flashflexpro.tanksinbattle.menuscene {
import com.flashflexpro.tanksinbattle.loader.LoadModuleEvent;
import com.flashflexpro.tanksinbattle.menuscene.dto.MenuItemDto;

import com.flashflexpro.tanksinbattle.scene.dto.FutureModuleInstDto;

import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;


[Event(name="switchToMenu", type="flash.events.Event")]
public class MenuItem extends MovieClip{
    
    public static const SWITCH_TO_MENU:String = "switchToMenu";

    public function MenuItem(menuItemDto:MenuItemDto) {
        _menuItemDto = menuItemDto;
        addEventListener( MouseEvent.CLICK, onClick );
        label.text = _menuItemDto.label;
        label.mouseEnabled = false;
    }

    private function onClick(event:MouseEvent):void {
        if( _menuItemDto.switchToModule != null ){
            dispatchEvent( new LoadModuleEvent( LoadModuleEvent.LOAD_NEXT_MODULE, _menuItemDto.switchToModule, _menuItemDto ) );
        }
        else if( _menuItemDto.switchToMenu != null ){
            dispatchEvent( new Event( SWITCH_TO_MENU, true ) )
        }
        else{
            throw new Error( "unknown menuitem!! is it the editor wrong??" );
        }
    }

    private var _menuItemDto:MenuItemDto;
    public function get menuItemDto():MenuItemDto {
        return _menuItemDto;
    }

    /**
     * won't work, because this is SimpleButton which only have 4 states can be drawn in Flash CS* 
     */
    public var label:TextField;

    public var button:SimpleButton;

}
}